package com.haikesi.api.excutionplan.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.haikesi.api.base.Constants;
import com.haikesi.api.base.Type;
import com.haikesi.api.base.User;
import com.haikesi.api.excutionplan.dao.ExcutionPlanDao;
import com.haikesi.api.excutionplan.domain.po.ExcutionPlanPO;
import com.haikesi.api.excutionplan.domain.vo.ExcutionPlanAddVO;
import com.haikesi.api.excutionplan.domain.vo.ExcutionPlanSearchVO;
import com.haikesi.api.excutionplan.domain.vo.ExcutionPlanUpdateVO;
import com.haikesi.api.excutionplan.domain.vo.ExcutionPlanVO;
import com.haikesi.api.excutionplan.service.IExcutionPlanService;
import com.haikesi.api.typetemplate.dao.TypeTemplateMapper;
import com.haikesi.exception.BusinessException;
import com.haikesi.util.ApcosApiUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class ExcutionPlanServiceImpl implements IExcutionPlanService {

    @Autowired
    ExcutionPlanDao dao;
    @Autowired
    private TypeTemplateMapper typeTemplateMapperDao;


    @Override
    public List<ExcutionPlanVO> getList(ExcutionPlanSearchVO excutionPlanSearchVO) {
        List<ExcutionPlanVO> list = dao.getList(excutionPlanSearchVO);
        return list;
    }

    @Override
    public ExcutionPlanPO addExcutionPlan(ExcutionPlanAddVO excutionPlanAddVO) {
        ExcutionPlanPO excutionPlanPO = new ExcutionPlanPO();
        excutionPlanPO.setId(UUID.randomUUID().toString());
        excutionPlanPO.setExcutionPlanEnable(excutionPlanAddVO.getExcutionPlanEnable());
        excutionPlanPO.setExcutionPlanName(excutionPlanAddVO.getExcutionPlanName());
        excutionPlanPO.setExcutionPlanNumber(excutionPlanAddVO.getAccount());

        String ser_code = "1";  // 执行计划请求参数：序号

        JSONObject jsonObject = ApcosApiUtils.addExcutionPlan(excutionPlanAddVO.getAccount(), excutionPlanAddVO.getExcutionPlanName(), excutionPlanAddVO.getExcutionPlanEnable().toString(), ser_code);

        if (jsonObject.get("resultContent") != null) {
            String id = jsonObject.get("resultContent").toString();
            excutionPlanPO.setPlatformId(id);
        } else {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
        User user = excutionPlanPO.getUser();
        //设置组织机构
        if (user != null && !user.getOrganizationId().isEmpty()) {
            excutionPlanPO.setOrganizationId(user.getOrganizationId());
        }
        excutionPlanPO.preInsert();
        int insert = dao.insert(excutionPlanPO);
        if (insert == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
        return excutionPlanPO;
    }

    @Override
    public void updateExcutionPlan(ExcutionPlanUpdateVO vo) {

//        Example example = new Example(ExcutionPlanPO.class);
//        example.and().andEqualTo("id", vo.getId());
//        List<ExcutionPlanPO> excutionPlanPOS = dao.selectByExample(example);
//        String platformId = excutionPlanPOS.get(0).getPlatformId();
        String platformId = vo.getPlatformId();

        JSONObject jsonObject = ApcosApiUtils.updateExcutionPlan(vo.getAccount(), vo.getExcutionPlanName(),
                vo.getExcutionPlanEnable().toString(), "1", platformId);
        if (!"0000".equals(jsonObject.get("resultCode").toString())) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }

        /*ExcutionPlanPO po = new ExcutionPlanPO();
        po.preUpdate();
        BeanUtils.copyProperties(vo, po);
        int i = dao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }*/
    }

    @Override
    public void delete(String id,String account) {
        JSONObject jsonObject = ApcosApiUtils.delExcutionPlan(account, id);
        if (!"0000".equals(jsonObject.get("resultCode").toString())) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }

        // 根据ID查询执行计划
        /*Example example = new Example(ExcutionPlanPO.class);
        example.and().andEqualTo("id", id).andEqualTo("delFlag", 0);

        List<ExcutionPlanPO> list = dao.selectByExample(example);
        ExcutionPlanPO excutionPlanPO = list.get(0);
        String platformId = excutionPlanPO.getPlatformId();
        String excutionPlanNumber = excutionPlanPO.getExcutionPlanNumber();

        JSONObject jsonObject = ApcosApiUtils.delExcutionPlan(excutionPlanNumber, platformId);
        if (!"0000".equals(jsonObject.get("resultCode").toString())) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
        excutionPlanPO.setDelFlag(true);
        int i = dao.updateByPrimaryKeySelective(excutionPlanPO);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }*/
    }
}
